<?php

namespace backend\modules\wechat\controllers;

use backend\controllers\AdminController;
use common\models\sys\SysSetting;
use common\models\wechat\WechatReplyKeyword;
use yii\db\Query;
use yii\helpers\ArrayHelper;

class SysReplyController extends AdminController
{

    public function actionIndex()
    {

        if(\Yii::$app->request->isPost){
            SysSetting::setKeyValue('wechat_default',\Yii::$app->request->post('default'));
            SysSetting::setKeyValue('wechat_welcome',\Yii::$app->request->post('welcome'));
            return $this->dialog('设置成功');
        }

        $default = SysSetting::getValue('wechat_default');
        $welcome = SysSetting::getValue('wechat_welcome');

        return $this->render('index',[
            'default' => $default,
            'welcome' => $welcome,
        ]);
    }


    public function actionSearchKwd()
    {
        $key_word = \Yii::$app->request->post('key_word');

        $query = (new Query())->select('content')->from(WechatReplyKeyword::tableName());

        if($key_word){
            $list = $query->where(['like','content',$key_word])
                ->all();
        }else{
            $list = $query->all();
        }

        $list = ArrayHelper::getColumn($list,'content');
        return json_encode($list);
    }


}


?>